import Service from '../../core/baseService';
import moment from 'moment';
import { Op } from 'sequelize';

export default class OrderService extends Service {
  constructor(ctx) {
    super(ctx);
    this.model = ctx.sassmodel.Order;
  }

  async business() {
    const data = await this.model.findAll({
      where: {
        pay_status: 1, // 支付成功
        create_at: {
          [Op.gt]: moment().startOf('day').unix(),
          [Op.lt]: moment().endOf('day').unix()
        }
      }
    });

    const waitPay = await this.model.findAll({
      where: {
        pay_status: 0, // 微支付
        create_at: {
          [Op.gt]: moment().startOf('day').unix(),
          [Op.lt]: moment().endOf('day').unix()
        }
      }
    });

    // 营业额
    const all_price = data.map(it => it.all_price).reduce((p, n) => p + n, 0);
    const noticeList = await this.ctx.sassmodel.Notice.findAll({
      where: {
        status: 1,
        is_delete: 0
      }
    })

    return {
      noticeList,
      order_number: data.length, // 订单总数
      instore_number: data.filter(it => it.order_type === 'INSTORE').length, // 外卖订单总数
      takeout_number: data.filter(it => it.order_type === 'TAKEOUT').length, // 堂食订单总数
      all_price,
      wait_number: waitPay.length, // 未支付订单
      data,
    };
  }




}
